package com.hb.game.pk.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseFragment;
import com.hb.game.pk.beans.BankInfo;
import com.hb.game.pk.beans.Login;
import com.hb.game.pk.beans.NullBean;
import com.hb.game.pk.event.ChangeHeadEvent;
import com.hb.game.pk.event.ClickTransEvent;
import com.hb.game.pk.event.MoneyChangeEvent;
import com.hb.game.pk.event.RefreshUserInfoEvent;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.ui.activity.login.LoginActivity;
import com.hb.game.pk.ui.activity.mine.BindActivity;
import com.hb.game.pk.ui.activity.mine.BindCardActivity;
import com.hb.game.pk.ui.activity.mine.MineMsgActivity;
import com.hb.game.pk.ui.activity.mine.RechargeActivity;
import com.hb.game.pk.ui.activity.mine.WebViewActivity;
import com.hb.game.pk.ui.activity.mine.WithdrawCashActivity;
import com.hb.game.pk.ui.activity.mine.customer_center.CenterWebActivity;
import com.hb.game.pk.ui.activity.mine.customer_center.CustomerCenterActivity;
import com.hb.game.pk.ui.activity.mine.info.InfoActivity;
import com.hb.game.pk.ui.activity.mine.recommend.RecommendActivity;
import com.hb.game.pk.ui.activity.mine.setting.SettingActivity;
import com.hb.game.pk.ui.activity.mine.wallet.WalletActivity;
import com.hb.game.pk.utils.SpUtils;
import com.hb.game.pk.utils.StringUtils;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.MineItemView;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MineFragment extends BaseFragment {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.rl_info)
    RelativeLayout rlInfo;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_tx)
    TextView tvTx;
    @BindView(R.id.tv_cz)
    TextView tvCz;
    @BindView(R.id.miv_tj)
    MineItemView mivTj;
    @BindView(R.id.miv_bank_card)
    MineItemView mivBankCard;
    @BindView(R.id.miv_tg)
    MineItemView mivTg;
    @BindView(R.id.miv_kf)
    MineItemView mivKf;
    @BindView(R.id.miv_setting)
    MineItemView mivSetting;
    @BindView(R.id.miv_msg)
    MineItemView mivMsg;
    @BindView(R.id.tv_ty_money)
    TextView tvTyMoney;
    @BindView(R.id.tv_wallert)
    TextView tvWallert;
    @BindView(R.id.tv_trans)
    TextView tvTrans;
    @BindView(R.id.tv_msg_count)
    TextView tvMsgCount;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        super.initView(view, savedInstanceState);
        Login.DataBean userInfo = APP.userInfo;
        String avatar = userInfo.getFace();
        String trim = StringUtils.getString(avatar);
        int res = 0;
        if (StringUtils.isEmpty(trim)) {
            res = R.mipmap.ic_head_normal;
        } else {
            int i = Integer.parseInt(trim);
            res = APP.headResource.get(i - 1);
        }
        ivHead.setBackgroundResource(res);
        tvNickname.setText(userInfo.getNickname());
        tvUsername.setText(String.format(mActivity.getResources().getString(R.string.mine_username), userInfo.getAccount()));
        tvMoney.setText(String.format(mActivity.getResources().getString(R.string.mine_money), userInfo.getMoney()));
        tvTyMoney.setText(String.format(mActivity.getResources().getString(R.string.mine_money), "0"));
        mivBankCard.setLeftText("银行卡");
        tvMsgCount.setVisibility(View.GONE);
    }

    @OnClick({R.id.rl_info, R.id.tv_tx, R.id.tv_cz, R.id.miv_tj, R.id.miv_bank_card, R.id.miv_tg, R.id.miv_kf, R.id.miv_setting, R.id.tv_wallert, R.id.tv_trans, R.id.miv_msg})
    public void onViewClicked(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.rl_info:
                intent.setClass(mActivity, InfoActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_tx:
                intent.putExtra("title", "提现");
                intent.setClass(mActivity, WithdrawCashActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_cz:
                intent.putExtra("title", "充值");
                intent.setClass(mActivity, RechargeActivity.class);
                startActivity(intent);
                break;
            case R.id.miv_tj:
                intent.setClass(mActivity, RecommendActivity.class);
                startActivity(intent);
                break;
            case R.id.miv_bank_card:
//                intent.setClass(mActivity, BindActivity.class);
                intent.setClass(mActivity, BindCardActivity.class);
                startActivity(intent);
                break;
            case R.id.miv_tg:
                intent.setClass(mActivity, WebViewActivity.class);
                startActivity(intent);
                break;
            case R.id.miv_kf:
				Uri uri = Uri.parse(Host.HOST + "kefuchat.html");
				intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
//                intent.setClass(mActivity, CenterWebActivity.class);
//                startActivity(intent);
                break;
            case R.id.miv_setting:
                intent.setClass(mActivity, SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_wallert:
                intent.setClass(mActivity, WalletActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_trans:
                EventBus.getDefault().post(new ClickTransEvent());
                break;
            case R.id.miv_msg:
                intent.setClass(mActivity, MineMsgActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        Call<Login> userinfo = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).userinfo();
        userinfo.enqueue(new Callback<Login>() {
            @Override
            public void onResponse(Call<Login> call, Response<Login> response) {
                Login body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }

                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }

                APP.userInfo = body.getData();
                tvNickname.setText(APP.userInfo.getNickname());
                SpUtils.putString(mActivity, "userInfo", new Gson().toJson(body.getData()));
            }

            @Override
            public void onFailure(Call<Login> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
        Call<NullBean> getmoney = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).getmoney();
        getmoney.enqueue(new Callback<NullBean>() {
            @Override
            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                NullBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() == 2) {
                    ToastUtils.showToast(body.getInfo());
                    APP.Token = "";
                    APP.uid = "";
                    APP.userInfo = null;
                    SpUtils.remove(mActivity, "uid");
                    SpUtils.remove(mActivity, "toke");
                    SpUtils.remove(mActivity, "userInfo");
                    Intent intent = new Intent(mActivity, LoginActivity.class);
                    startActivity(intent);
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                if (body.getData() != null)
                    tvMoney.setText(String.format(mActivity.getResources().getString(R.string.mine_money), (Float.parseFloat(body.getData().toString()) / 100f) + ""));
            }

            @Override
            public void onFailure(Call<NullBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
        Call<NullBean> getmoney1 = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST_TY).getmoney();
        getmoney1.enqueue(new Callback<NullBean>() {
            @Override
            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                NullBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                if (body.getData() != null)
                    tvTyMoney.setText(String.format(mActivity.getResources().getString(R.string.mine_money), (Float.parseFloat(body.getData().toString()) / 100f) + ""));
            }

            @Override
            public void onFailure(Call<NullBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });

        Call<BankInfo> bank = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).get_bank();
        bank.enqueue(new Callback<BankInfo>() {
            @Override
            public void onResponse(Call<BankInfo> call, Response<BankInfo> response) {
                BankInfo body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                APP.bankInfo = body.getData();
            }

            @Override
            public void onFailure(Call<BankInfo> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
        getMsgCount();
    }

    private void getMsgCount() {
        Call<NullBean> messagenumber = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).messagenumber();
        messagenumber.enqueue(new Callback<NullBean>() {
            @Override
            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                NullBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }

                String count = (String) body.getData();
                count = count.replace(".0", "");
                tvMsgCount.setVisibility(View.VISIBLE);
                tvMsgCount.setText(count);
            }

            @Override
            public void onFailure(Call<NullBean> call, Throwable t) {

            }
        });
    }

    @Subscribe
    public void onEvent(ChangeHeadEvent e) {
        ivHead.setBackgroundResource(e.res);
    }

    @Subscribe
    public void onEvent(RefreshUserInfoEvent e) {
        onRefreshData();
    }

    @Subscribe
    public void onEvent(MoneyChangeEvent e) {
        onRefreshData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
